package icu.sunway.gushenBlogBackEnd.Service.Implement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.sunway.gushenBlogBackEnd.Entity.Data.ProjectDO;
import icu.sunway.gushenBlogBackEnd.Entity.View.Project.ProjectPostVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.Project.ProjectUpdateVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.ResultVO;
import icu.sunway.gushenBlogBackEnd.Mapper.ProjectMapper;
import icu.sunway.gushenBlogBackEnd.Message.ProjectMessage;
import icu.sunway.gushenBlogBackEnd.Service.ProjectService;
import icu.sunway.gushenBlogBackEnd.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, ProjectDO> implements ProjectService {

    @Override
    public ResultVO<List<ProjectDO>> getProjects() {
        QueryWrapper<ProjectDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("publish_time");
        return new ResultVO<>(200, ProjectMessage.get_project_success, list(queryWrapper));
    }

    @Override
    public ResultVO<List<ProjectDO>> getProjectListByPage(int curPage, int pageSize) {
        Page<ProjectDO> page = new Page<>(curPage, pageSize);
        QueryWrapper<ProjectDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("publish_time");
        IPage<ProjectDO> projectDOIPage = page(page, queryWrapper);
        List<ProjectDO> projectDOList = projectDOIPage.getRecords();
        return new ResultVO<>(200, ProjectMessage.get_project_success, projectDOList);
    }

    @Override
    public ResultVO<Long> getProjectCount() {
        long count = count(null);
        return new ResultVO<>(200, ProjectMessage.get_count_success, count);
    }

    @Override
    public ResultVO<Boolean> addProject(ProjectPostVO projectPostVO) {
        try {
            save(new ProjectDO(null,
                    projectPostVO.getTitle(),
                    projectPostVO.getContent(),
                    projectPostVO.getLink(),
                    DateUtils.getCurDateString()));
        } catch (Exception e) {
            return new ResultVO<>(400, ProjectMessage.add_project_fail, false);
        }
        return new ResultVO<>(200, ProjectMessage.add_project_success, true);
    }

    @Override
    public ResultVO<Boolean> removeProject(String id) {
        try {
            removeById(id);
        } catch (Exception e) {
            return new ResultVO<>(400, ProjectMessage.remove_project_fail, false);
        }
        return new ResultVO<>(200, ProjectMessage.remove_project_success, true);
    }

    @Override
    public ResultVO<Boolean> updateProject(ProjectUpdateVO projectUpdateVO) {
        try {
            UpdateWrapper<ProjectDO> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", projectUpdateVO.getId());
            updateWrapper.set("title", projectUpdateVO.getTitle());
            updateWrapper.set("content", projectUpdateVO.getContent());
            updateWrapper.set("link", projectUpdateVO.getLink());
            update(updateWrapper);
        } catch (Exception e) {
            return new ResultVO<>(400, ProjectMessage.update_project_fail, false);
        }
        return new ResultVO<>(200, ProjectMessage.update_project_success, true);
    }
}
